import { ref } from 'vue';
import { apiGetOrderDetail } from '@/api/dispatch';
import { useRoute } from 'vue-router';

export default function useTaskDetail() {
  const route = useRoute();
  const formModel = ref<any>({});
  const showPage = ref(false);

  const showForm = ref<string[]>([]);

  const getEditForm = () => {
    const { state } = formModel.value;
    if (state === 1) {
      showForm.value = ['1'];
      return;
    }

    // 待调度
    if (state === 2) {
      showForm.value = ['1', '2'];
      return;
    }

    // 审核驳回
    if (state === 3) {
      showForm.value = ['1', '2'];
      return;
    }

    // 待确认
    // if (state === 4) {
    //   if (formModel.value.dispatch_type === '普通调度') {
    //     showForm.value = ['1', '2', '3'];
    //     return;
    //   }
    //   if (formModel.value.dispatch_type === '应急调度') {
    //     showForm.value = ['1', '3'];
    //     return;
    //   }
    // }

    // 待开始
    if (state === 5) {
      if (formModel.value.dispatch_type === '普通调度') {
        showForm.value = ['1', '2', '3', '4'];
        return;
      }
      if (formModel.value.dispatch_type === '应急调度') {
        showForm.value = ['1', '3', '4'];
        return;
      }
    }
    // 进行中
    if (state === 6) {
      if (formModel.value.dispatch_type === '普通调度') {
        showForm.value = ['1', '2', '3', '4', '5'];
        return;
      }
      if (formModel.value.dispatch_type === '应急调度') {
        showForm.value = ['1', '3', '4', '5'];
      }
    }

    // 已完结
    if (state === 7) {
      if (formModel.value.dispatch_type === '普通调度') {
        showForm.value = ['1', '2', '3', '4', '5', '9'];
        return;
      }
      if (formModel.value.dispatch_type === '应急调度') {
        showForm.value = ['1', '3', '4', '5', '9'];
      }
    }

    // 已取消
    if (state === 8) {
      if (formModel.value.dispatch_type === '普通调度') {
        showForm.value = ['1', '2', '3', '4', '5'];
        return;
      }
      if (formModel.value.dispatch_type === '应急调度') {
        showForm.value = ['1', '3', '4', '5'];
      }
    }
  };

  const getDetail = () => {
    apiGetOrderDetail({
      id: route.query.id,
    })
      .then((res) => {
        formModel.value = {
          order_details: res.data.order_details || {},
          order_approve_details: res.data.order_approve_details || {},
          order_dispatch_details: res.data.order_dispatch_details || {},
          driver_details: res.data.driver_details || {},
          order_execute_details: res.data.order_execute_details || {},
          order_bill_details: res.data.order_bill_details || {},
        };

        formModel.value.dispatch_type =
          formModel.value.order_details.dispatch_type;
        formModel.value.state = formModel.value.order_details.state;

        showPage.value = true;

        // 状态（1 待审核，2 待调度，3 审核驳回，4 待确认，5 待开始，6 进行中，7 已完结，8 已取消）
        getEditForm();
      })
      .catch((err) => {
        console.log('🚀 ~ getDetail ~ err:', err);
      });
  };

  return {
    formModel,
    showPage,
    showForm,
    getDetail,
  };
}
